﻿using Sasa.QualityTools.CodeAnalysis.Core.Requests;
using Sasa.QualityTools.CodeAnalysis.Core.Responses;
using Sasa.QualityTools.CodeAnalysis.Core.Services.CodeAnalysis;
using System;
using Sasa.QualityTools.CodeAnalysis.Common;
using System.ComponentModel;

namespace Sasa.QualityTools.CodeAnalysis.Core.Services
{
    public class CodeAnalysisService
    {
        public CodeAnalysisResponse Execute(CodeAnalysisRequest request)
        {
            if (request == null) throw new ArgumentNullException("request");

            var analyzer = CreateCodeAnalyzer(request);
            var report = analyzer.Analyze();

            return new CodeAnalysisResponse(report.Problems, report.Rules);
        }

        internal CodeAnalyzerBase CreateCodeAnalyzer(CodeAnalysisRequest request)
        {
            if (request is AssemblyAnalysisRequest)
            {
                return new AssemblyAnalyzer((AssemblyAnalysisRequest)request);
            }
            else if (request is FxCopProjectAnalysisRequest)
            {
                return new FxCopProjectAnalyzer((FxCopProjectAnalysisRequest)request);
            }
            else
            {
                throw new InvalidOperationException("Failed to execute code analysis because the specified configuration type '" + request.GetType().FullName + "' isn't supported.");
            }
        }
    }
}
